#include <stdio.h>

#define rows 28
#define cols 14
#define active 2
#define inactive 1
#define free 0

int checkForCollision(int board[rows][cols], int row, int col);
int copyBoard(int board [][], int copyOfBoard[][]);
int oneSquareDown();
void printArray(int board[rows][cols]);
void fixActiveBlock(int board[rows][cols]);
void eliminateRows(int board[rows][cols]);
void removeActiveBlock(int board[rows][cols]);

int x = 99, board[rows][cols], row = 0,col=0,copyOfBoard[rows][cols], score=0,rowComplete = 0;
typedef struct
{
  int x;
  int y;
  int shape;
  int angle;
} ActiveBlock;
ActiveBlock activeBlock= {
      0, 0, 0,0
      };

int main()
{
	
  printf ("%d %d %d\n", board[5][5], x, row);
  for (row = 0; row < rows; row++)
  {
	for(col =0; col<cols;col++){
		board[row][col] = free;
	}
  }
  
  board[26][0]=active;
  board[26][1]=active;
  board[26][2]=active;
  board[27][1]=active;
  board[27][0]=inactive;
  board[27][2]=inactive;
  board[27][3]=inactive;
  board[27][4]=inactive;
  board[27][5]=inactive;
  board[27][6]=inactive;
  board[27][7]=inactive;
  board[27][8]=inactive;
  board[27][9]=inactive;
  board[27][10]=inactive;
  board[27][11]=inactive;
  board[27][12]=inactive;
  board[27][13]=inactive;
  board[0][0]=inactive;
  
  printArray(board);
  oneSquareDown();
  printArray(board);
  printf ("%d %d %d\n", board[2][1], x, row);
  return 0;
}

int copyBoard(int board1 [][cols], int board2[][cols]){
	for (row = 0; row < rows; row++){
		for(col =0; col<cols;col++){
			board2[row][col]=board1[row][col];
		}
	}
	return 0;
}

int oneSquareDown(){
	copyBoard(board,copyOfBoard);
	
	for (row = rows; row >= 0; row--){
		for(col =0; col<cols;col++){
			if(copyOfBoard[row][col]==active){
				if (!checkForCollision(copyOfBoard, row+1, col)) {
					fixActiveBlock(board);
					/*find rows and eliminate them*/
   					eliminateRows(board);
					/*
   					// start new round if not possible game is lost
					if (!startNewRound(board)) {
   						 gameLost();
   					}*/
					printf("collision\n");
   					return 0;
   				}
				printf("test\n");
				/*copy active square one field dowon*/
   				copyOfBoard[row + 1][col] = copyOfBoard[row][col];
				copyOfBoard[row][col]=free;
   			 }
   		 }
   	 }
	 	/*update coordinates
activeBlockX=1+activeBlockX;*/
	activeBlock.y=activeBlock.y+1;
	copyBoard(copyOfBoard,board);
	 return 0;
}

int checkForCollision(int board[rows][cols], int row, int col) {
	printf ("%d %d %d\n", row, col, board[row][col] != inactive);
   	if (row <= 27 && (board[row][col] != inactive) && col >= 0 && col < 14){
   		 return 1;
   	}
   	return 0;
}

void printArray(int board[rows][cols]){
printf("\n");
	for (row = 23; row < rows; row++){
	printf (" %d ", row);
		for(col =0; col<cols;col++){
			printf (" %d ", board[row][col]);
		}
		printf("\n");
	}
}

void fixActiveBlock(int board[rows][cols]){
	for (row = 0; row < rows; row++){
		for(col =0; col<cols;col++){
			if(board[row][col]==active){
			
				board[row][col]=inactive;
			}
		}
	}
}

void eliminateRows(int board[rows][cols]){
/*check row by row if one is completed*/
   	 for (row = 0; row < rows; row++) {
   		 rowComplete = 1;
   		 for (col = 0; col < cols; col++) {
   			 if (board[row][col] != inactive) {
   				rowComplete = 0;
   				break;
   			 }
   		 }
		/*if row is completed move all blocks above one field down*/
   		 if (rowComplete) {
		 int i;
		 printf("row complete\n");
   			 for (i = row; i >= 1; i--) {
   				 for (col = 0; col < cols; col++) {
   					board[i][col] = board[i - 1][col];
   				 }
   			 }
			 for (col = 0; col < cols; col++) {
   					board[0][col] = 0;
   				}
			score=score+100;
   		 }
   	}
}

void startNewRound(int board[rows][cols]){
	
}

void flipClockwise() {
	copyBoard(board,copyOfBoard);
   	removeOldActiveBlock(copy_of_board);

   	//also updates activeBlock structure
   	int[][] newBlock = getNewBlock(active, angle);	//copy block into board
   	for (int row = 0; row < newBlock.length; row++) {
   		for (int col = 0; col < newBlock[0].length; col++) {
   			if (newBlock[row][col] == active) {
   				// check for collision.
   				if (board[row + activeBlockX][col + activeBlockY] != 						inactive) {
   					// if collision detected ignore command
   					return;
   				 }
   				board[row + activeBlockX][col + activeBlockY]= active;
   			 }
   		 }
   	 }
   	 board=copy_of_board;
    }

void removeActiveBlock(int board[rows][cols]){
	for (int row = activeBlock.y; row < activeBlock.y + 6; row++) {
   		for (int col = activeBlock.x; col < activeBlock.x + 6; col++) {
			if (board[row][col] == active) {
				board[row][col] == free;
			}
		}
	}
}

getNewBlock(active, angle);